package com.aat.restful.jersey.demo2.netty;

import com.aat.restful.jersey.demo2.netty.JerseyServletChannelInitializer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class JerseyServer {

	public void start(int port){
		int ts = Runtime.getRuntime().availableProcessors();//CPU核心数
		ServerBootstrap bootstrap = new ServerBootstrap();
		NioEventLoopGroup boss = new NioEventLoopGroup(ts);
		NioEventLoopGroup work = new NioEventLoopGroup(ts * 2);
		try {
			bootstrap.group(boss, work);
			bootstrap.channel(NioServerSocketChannel.class);
			bootstrap.localAddress(port);
			bootstrap.childHandler(new JerseyServletChannelInitializer());// channel
			bootstrap.bind().sync().channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			boss.shutdownGracefully();
			work.shutdownGracefully();
		}
	}

	public static void main(String[] args) {
		try {
			JerseyServer server= new JerseyServer();
			server.start(8082);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
